	
Ext.data.PropertyReader = Ext.extend(Ext.data.DataReader,{
	propertySeparator: "=",	
	read: function(response){
		var propertyFile = response.responseText;
		if(!propertyFile)
			throw {message: "PropertyReader.read: File not found"};
						
		return this.readRecords(propertyFile);
	},
	
	readRecords: function(propertyFile){
		var totalRecords = 0, success = true;
		var records = [];
		
		var f = this.readLines(propertyFile);
		for(var i = 0; i < f.length; i++){
			var key, key;
			kLim = f[i].indexOf(this.propertySeparator);
			key = String(f[i].substring(0, kLim));
			value = eval(f[i].substring(kLim+1));
			var record = new Ext.data.Record(value, key);
			records[i] = record;
		}
		return {
	        success : success,
	        records : records,
	        totalRecords : f.length || totalRecords
	    };
	
	},
	
	//private
	readLines: function(file){
		var aux = String(file).split('\n');
		var lines = new Array();
		
		for(var i = 0; i < aux.length; i++){
			if(aux[i].indexOf("#") < 0 || (aux[i].indexOf("#") < aux[i].indexOf("\""))){
				line = Ext.util.Format.trim(aux[i]);
				if(line.length > 0 )
					lines.push(Ext.util.Format.trim(aux[i]));
			}	
		}
		return lines;
	}

});


Ext.namespace('Ext.i18n');

Ext.i18n.Bundle = function(config){
	this.bundle = config.bundle;
	this.path = config.path;
	this.language = config.defaultLanguage;
	var url;
	if(this.path)
		url = this.path + '/';
	url+=this.bundle+'_'+this.language+this.resourceExt;
    Ext.i18n.Bundle.superclass.constructor.call(this, {
        proxy: new Ext.data.HttpProxy({
								url: __ctxPath + url, 
								method: 'POST'
								}),
        reader: new Ext.data.PropertyReader()
    });

	this.load();
	this.on('loadexception', this.loadParent);
};

 Ext.extend(Ext.i18n.Bundle, Ext.data.Store,{ 
	defaultLanguage: 'zh-cn',
	loadFlag: false,
	resourceExt: '.properties',
	bundle: '',
	path: null,
	
	//private
	guessLanguage: function(){
		return this.defaultLanguage;//navigator.language || navigator.browserLanguage|| navigator.userLanguage ||
				
	},
	
	getMsg: function(key){
		return this.getById(key)? this.getById(key).data : 'undefined';
	},
	
	onReady: function(fn){
		this.readyFn = fn;
		this.on('load', this.readyFn);
	},
	
	loadParent: function(){
		if(!this.loadFlag){
			this.loadFlag=true;
			var url;
			if(this.path)
				url = this.path + '/';
			url+=this.bundle+this.resourceExt;
			this.proxy = new Ext.data.HttpProxy({
								url:__ctxPath + url, 
								method: 'POST'
								});
			this.load();			
		}else{
			throw {message: 'Resource Bundle not found'};
		}	
	}

});

